import { ChangeDetectorRef, Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { UtilService } from '@core/services/util.service';

@Component({
  selector: 'app-operation-person-add',
  templateUrl: './operation-person-add.component.html',
  styles: []
})
export class OperationPersonAddComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @Input()type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.schema.properties.userName.readOnly = true
    this.param = value;
    this.param.password = ""
    this.param.areaCityAreaId = value.areaCityAreaList
  };
  param: any;
  value:any;
  nodes=[];
  timeout:any="";
  show:boolean=true;
  showForm = false;
  isLoading:boolean=false;
  schema: SFSchema = {
    properties: {
      userName: {
        type: 'string',
        title: '账号',
        readOnly:false
      },
      password: {
        type: 'string',
        title: '密码',
        minLength:6,
        // format:'regex',
        // pattern:'/[a-zA-Z0-9]{6,}/',
      },
      type:{
        type:"string",
        title:"人员类型",
        enum:[
          {label:"运维管理人员",value:1},
          {label:"物业人员",value:2}
        ],
        ui:{
          widget:'select'
        }
      },
      areaCityAreaId: {
        type: 'string',
        title: '选择小区',
        enum: [],
        ui: {
          widget: 'select',
          mode: 'multiple',
          dropdownStyle: {
            'max-height': '300px'
          }
        }
      }
    },
    required: ['userName','type', 'areaCityAreaId']
  }

  formSubmit(data) {
    const userNow =JSON.parse(this.util.getStorage("userNow"))
    const body: any = {
      userName : data.userName,
      password : data.password,
      type : data.type,
      areaCityAreaId:data.areaCityAreaId,
      createPeople : userNow.userAccount,
      createPeopleId : userNow.id
    }
    let url = '';
    if(this.type == 0){
      url = "/core/qr/manager/save"
    }else if(this.type == 1){
      url = "/core/qr/manager/update"
      body.id = data.id
      delete body.userName
      delete body.createPeople
      delete body.createPeopleId
      if(!data.password){
        delete body.password
      }
    }
    this.http.request<any>("post", url, {body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })

  }
  //查询小区
  queryArea():void{
    this.http.request<any>("post","/core/city/area/list",{body:{"areaRank":"6"}}).subscribe(res=>{
      console.log(res)
      if(res.status == 200){
        this.schema.properties.areaCityAreaId.enum = res.data.map(item => {
          return {label: item.areaName, value: item.id}
        })
        this.showForm = true;
      }else{
        this.msg.error(res.message)
      }
    })
  }

  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private util: UtilService,
    private ref: ChangeDetectorRef,
  ) { }

  async ngOnInit() {
    
    this.queryArea()
    
  }

}
